﻿Public Class frmEditCalendar

    Private lift As Integer = 0
    Private tooltip As New System.Windows.Forms.ToolTip
    Friend WithEvents CalendarCategory As ComboIcon
    Private Sub frmEditCalendar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ETimeStart1.Text = "00"
        ETimeEnd1.Text = "00"
        EDuration1.Text = "00"
        ETimeStart2.Text = "00"
        ETimeEnd2.Text = "00"
        EDuration2.Text = "00"
        HiddenBox.Visible = False
        EradioNone.Checked = True
        Me.Height = 437
        ECheckAlarm.Checked = False
        EComboAlarm.Enabled = False
        Me.CalendarCategory = New ComboIcon
        '
        'CalendarCategory
        '
        Me.CalendarCategory.DisplayMember = "       < Unfiled >"
        Me.CalendarCategory.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
        Me.CalendarCategory.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.CalendarCategory.FormattingEnabled = True
        Me.CalendarCategory.ItemHeight = 16
        Me.CalendarCategory.Items.AddRange(New Object() {"       < Unfiled >"})
        Me.CalendarCategory.Location = New System.Drawing.Point(423, 370)
        Me.CalendarCategory.Name = "CalendarCategory"
        Me.CalendarCategory.Size = New System.Drawing.Size(212, 22)
        Me.CalendarCategory.TabIndex = 18
        Me.CalendarCategory.ValueMember = "       < Unfiled >"
        Me.CalendarCategory.SelectedIndex = 0
        Me.Controls.Add(Me.CalendarCategory)
        ' Me.CalendarCategory.t
    End Sub

    Private Sub EscCalendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EscCalendar.Click


    End Sub

    Private Sub OKCalendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKCalendar.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim frm1 As New frmEditCalendars
        Me.AddOwnedForm(frm1)
        frm1.ShowDialog()
    End Sub

    Private Sub Track_solve(ByVal flag As Boolean)
        Dim a1 As Integer
        Dim a2 As Integer
        Dim b1 As Integer
        Dim b2 As Integer
        Dim dura As Integer
        Dim dura1 As Integer
        Dim dura2 As Integer

        a1 = 23 - TrackBar1.Value
        a2 = (11 - TrackBar2.Value) * 5
        b1 = 24 - TrackBar3.Value
        b2 = (11 - TrackBar4.Value) * 5

        If a1 > b1 Then
            TrackBar3.Value = TrackBar1.Value + 1
            TrackBar3.Update()
            If a2 > b2 Then
                TrackBar4.Value = TrackBar2.Value
                TrackBar4.Update()
            End If
        ElseIf a1 = b1 Then
            If a2 > b2 Then
                TrackBar4.Value = TrackBar2.Value
                TrackBar4.Update()
            End If
        End If

        If b1 = 24 Then
            TrackBar4.Value = 11
        End If

        If (flag) Then
            dura1 = Integer.Parse(EDuration1.Text)
            dura2 = Integer.Parse(EDuration2.Text)

            If (23 - TrackBar1.Value + dura1) * 60 + (11 - TrackBar2.Value) * 5 + dura2 >= 1440 Then
                TrackBar3.Value = 0
                TrackBar4.Value = 11
            Else
                TrackBar3.Value = 1 + TrackBar1.Value - dura1
                dura = TrackBar2.Value - Int(dura2 / 5)
                If dura >= 0 Then
                    TrackBar4.Value = dura
                Else
                    TrackBar4.Value = 12 + dura
                    TrackBar3.Value -= 1
                End If
            End If
        End If

        a1 = 23 - TrackBar1.Value
        a2 = (11 - TrackBar2.Value) * 5
        b1 = 24 - TrackBar3.Value
        b2 = (11 - TrackBar4.Value) * 5

        ETimeStart1.Text = Update_E_Time(a1)
        ETimeStart2.Text = Update_E_Time(a2)
        ETimeEnd1.Text = Update_E_Time(b1)
        ETimeEnd2.Text = Update_E_Time(b2)

        dura = (b1 - a1) * 60 + b2 - a2

        a1 = Int(dura / 60)
        a2 = dura - a1 * 60

        EDuration1.Text = Update_E_Time(a1)
        EDuration2.Text = Update_E_Time(a2)
    End Sub

    Private Sub Duration_Change(ByVal flag As Boolean)
        Dim a1 As Integer
        Dim a2 As Integer
        Dim b1 As Integer
        Dim b2 As Integer

        a1 = 23 - TrackBar1.Value
        a2 = (11 - TrackBar2.Value) * 5
        b1 = 24 - TrackBar3.Value
        b2 = (11 - TrackBar4.Value) * 5

        If flag = True Then
            If TrackBar4.Value > 0 And TrackBar3.Value <> 0 Then
                TrackBar4.Value -= 1
            Else
                If TrackBar3.Value > 0 Then
                    TrackBar3.Value -= 1
                    TrackBar4.Value = 11
                ElseIf TrackBar3.Value = 0 Then
                    TrackBar4.Value = 11
                End If
            End If
        Else
            If b1 > a1 Then
                If b2 > 0 Then
                    TrackBar4.Value += 1
                Else
                    TrackBar3.Value += 1
                    TrackBar4.Value = 0
                End If
            Else
                If b2 > a2 Then
                    TrackBar4.Value += 1
                End If
            End If
        End If
    End Sub

    Private Function Update_E_Time(ByVal a1 As Integer) As String
        Dim str As String

        If a1 < 10 Then
            str = "0" & a1.ToString
        Else
            str = a1.ToString
        End If
        Return str
    End Function

    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        Track_solve(True)
        tooltip.SetToolTip(TrackBar1, 23 - TrackBar1.Value)
    End Sub

    Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
        Track_solve(True)
        tooltip.SetToolTip(TrackBar2, 11 - TrackBar2.Value)
    End Sub

    Private Sub TrackBar3_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar3.Scroll
        Track_solve(False)
        tooltip.SetToolTip(TrackBar3, 24 - TrackBar3.Value)
    End Sub

    Private Sub TrackBar4_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar4.Scroll
        Track_solve(False)
        tooltip.SetToolTip(TrackBar4, 11 - TrackBar4.Value)
    End Sub

    Private Sub EventIncrease_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EventIncrease.Click
        Duration_Change(True)
        Track_solve(False)
    End Sub

    Private Sub EventReduce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EventReduce.Click
        Duration_Change(False)
        Track_solve(False)
    End Sub

    Private Sub ETimeStart1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ETimeStart1.LostFocus
        Dim str As String
        Dim a As Integer
        str = ETimeStart1.Text
        If Integer.TryParse(str, a) Then
            a = Integer.Parse(str)
            If a >= 0 And a < 24 Then
                TrackBar1.Value = 23 - a
            ElseIf a >= 24 Then
                TrackBar1.Value = 0
            ElseIf a < 0 Then
                TrackBar1.Value = 23
            End If
        End If
        Track_solve(False)
    End Sub

    Private Sub ETimeStart2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ETimeStart2.LostFocus
        Dim str As String
        Dim a As Integer
        str = ETimeStart2.Text
        If Integer.TryParse(str, a) Then
            a = Integer.Parse(str)
            If 0 <= a And a <= 55 Then
                If a <> 55 Then
                    a = Int(a / 5) * 5 + 5
                End If
                TrackBar2.Value = 11 - Int(a / 5)
            ElseIf a < 0 Then
                TrackBar2.Value = 11
            ElseIf a > 55 Then
                TrackBar2.Value = 0
            End If
        End If
        Track_solve(False)
    End Sub

    Private Sub ETimeEnd1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ETimeEnd1.LostFocus
        Dim str As String
        Dim a As Integer
        str = ETimeEnd1.Text
        If Integer.TryParse(str, a) Then
            a = Integer.Parse(str)
            If a >= (23 - TrackBar1.Value) And a < 25 Then
                TrackBar3.Value = 24 - a
            ElseIf a > 24 Then
                TrackBar3.Value = 0
            ElseIf a < 0 Then
                TrackBar3.Value = 24
            End If
        End If
        Track_solve(False)
    End Sub

    Private Sub ETimeEnd2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ETimeEnd2.LostFocus
        Dim str As String
        Dim a As Integer
        str = ETimeEnd2.Text
        If Integer.TryParse(str, a) Then
            a = Integer.Parse(str)
            If TrackBar1.Value > TrackBar3.Value Then
                If 0 <= a And a <= 55 Then
                    If a <> 55 Then
                        a = Int(a / 5) * 5 + 5
                    End If
                    TrackBar4.Value = 11 - Int(a / 5)
                ElseIf a < 0 Then
                    TrackBar4.Value = 11
                ElseIf a > 55 Then
                    TrackBar4.Value = 0
                End If
            Else
                If 0 <= a And a <= (11 - TrackBar2.Value) * 55 Then
                    If a <> 55 Then
                        a = Int(a / 5) * 5 + 5
                    End If
                    TrackBar4.Value = 11 - Int(a / 5)
                ElseIf a < 0 Then
                    TrackBar4.Value = 11
                ElseIf a > (11 - TrackBar2.Value) * 55 Then
                    TrackBar4.Value = TrackBar2.Value
                End If
            End If
        End If
        Track_solve(False)
    End Sub

    Private Sub EDuration1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EDuration1.LostFocus
        Dim str1 As String
        Dim str2 As String
        Dim c1 As Integer
        Dim c2 As Integer
        Dim b1 As Integer
        Dim b2 As Integer
        Dim a1 As Integer
        Dim a2 As Integer
        Dim dura1 As Integer

        a1 = 23 - TrackBar1.Value
        'a2 = (11 - TrackBar2.Value) * 5
        b1 = 24 - TrackBar3.Value
        'b2 = (11 - TrackBar4.Value) * 5

        str1 = EDuration1.Text
        str2 = EDuration2.Text
        If Integer.TryParse(str1, c1) And Integer.TryParse(str1, c2) Then
            If (0 <= c1 And c1 <= 24) Or (c1 = 24 And c2 = 0) Then
                dura1 = (a1 + c1) * 60 + (a2 + c2)
                If dura1 > 1440 Then
                    TrackBar3.Value = 0
                    TrackBar4.Value = 11
                Else
                    a2 = c1 + a1
                    b2 = c2 + a2
                    If b2 > 55 Then
                        b2 -= 55
                        a2 += 1
                    End If
                    TrackBar3.Value = 24 - a2
                    TrackBar4.Value = Int(b2 / 5)
                End If
            End If
        End If
        Track_solve(False)
    End Sub

    Private Sub EDuration2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EDuration2.LostFocus
        Dim str1 As String
        Dim str2 As String
        Dim c1 As Integer
        Dim c2 As Integer
        Dim b1 As Integer
        Dim b2 As Integer
        Dim a1 As Integer
        Dim a2 As Integer
        Dim dura1 As Integer

        a1 = 23 - TrackBar1.Value
        'a2 = (11 - TrackBar2.Value) * 5
        b1 = 24 - TrackBar3.Value
        'b2 = (11 - TrackBar4.Value) * 5

        str1 = EDuration1.Text
        str2 = EDuration2.Text
        If Integer.TryParse(str1, c1) And Integer.TryParse(str1, c2) Then
            If (c1 = 24 And c2 = 0) Or (c1 < 24) Then
                If c2 < 0 Then
                    c2 = 0
                ElseIf c2 > 55 Then
                    c2 = 55
                End If

                dura1 = (a1 + c1) * 60 + (a2 + c2)
                If dura1 > 1440 Then
                    TrackBar3.Value = 0
                    TrackBar4.Value = 11
                Else
                    a2 = c1 + a1
                    b2 = c2 + a2
                    If b2 > 55 Then
                        b2 -= 55
                        a2 += 1
                    End If
                    TrackBar3.Value = 24 - a2
                    TrackBar4.Value = Int(b2 / 5)
                End If
            End If
        End If
        Track_solve(False)
    End Sub

    Private Sub EradioNone_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EradioNone.CheckedChanged
        HiddenBox.Visible = False
        Me.Height = 437
    End Sub

    Private Sub EradioDaily_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EradioDaily.CheckedChanged
        HiddenBox.Visible = True
        Me.Height = 571
        PanelDaily.Visible = True
        PanelMonthly.Visible = False
        PanelWeekly.Visible = False
        PanelYearly.Visible = False
        If ETimeStart.Enabled = False Then
            ETimeStart.Enabled = True
        End If
    End Sub

    Private Sub EradioWeekly_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EradioWeekly.CheckedChanged
        If HiddenBox.Visible = False Then
            HiddenBox.Visible = True
        End If
        Me.Height = 571
        PanelDaily.Visible = False
        PanelMonthly.Visible = False
        PanelWeekly.Visible = True
        PanelYearly.Visible = False
        If ETimeStart.Enabled = False Then
            ETimeStart.Enabled = True
        End If
    End Sub

    Private Sub EradioMonthly_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EradioMonthly.CheckedChanged
        If HiddenBox.Visible = False Then
            HiddenBox.Visible = True
        End If
        Me.Height = 571
        PanelDaily.Visible = False
        PanelMonthly.Visible = True
        PanelWeekly.Visible = False
        PanelYearly.Visible = False
        If ETimeStart.Enabled = False Then
            ETimeStart.Enabled = True
        End If
    End Sub

    Private Sub EradioYearly_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EradioYearly.CheckedChanged
        If HiddenBox.Visible = False Then
            HiddenBox.Visible = True
        End If
        Me.Height = 571
        PanelDaily.Visible = False
        PanelMonthly.Visible = False
        PanelWeekly.Visible = False
        PanelYearly.Visible = True
        ETimeStart.Enabled = False
    End Sub


    Private Sub ECheckAlarm_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ECheckAlarm.CheckedChanged
        If ECheckAlarm.Checked Then
            EComboAlarm.Enabled = True
        Else
            EComboAlarm.Enabled = False
        End If
    End Sub

    Private Sub CalendarCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalendarCategory.SelectedIndexChanged
        If Me.CalendarCategory.ColorList.Count > 0 Then
            CalendarComboColor.BackColor = Me.CalendarCategory.ColorList.Item(Me.CalendarCategory.SelectedIndex)
        Else
            CalendarComboColor.BackColor = Color.White
        End If

        'MessageBox.Show(Me.CalendarCategory.SelectedIndex & " - " & Me.CalendarCategory.ColorList.Count)
    End Sub
End Class